#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
void Swap(char*s,int i,int j)
{
    char temp = s[i];
    s[i] = s[j];
    s[j] = temp;
}

void generateKey(char*key)
{
    for (int i = 0;i < 26; i++)
    {
        key[i] = 'a' + i;
    }
    
    srand(time(NULL));
    for(int i = 25;i > 0;i--)
    {
        int j = rand()% i;
        Swap(key,i,j);
    }
    printf("生成的key:%s\n",key);
}

void Encrypt(char *msg,char *key)
{
    for(int i = 0;i < strlen(msg);i++)
    {   
        if(msg[i] <= 'z' && msg[i] >= 'a')
        msg[i] = key[msg[i] - 'a'];
    }
}

void Decrypt(char *msg,char*key)
{
    for(int i = 0;i < strlen(msg);i++)
    {
         if(msg[i] <= 'z' && msg[i] >= 'a')
         {
            for(int j = 0;j < 26;j++)
            {
                if(msg[i] == key[j])
                {
                    msg[i] = 'a' +j;
                    break;

                }
            }
         }
    }
}

int main()
{   
    char key[26] = "sfkumhxwlcnogrpbtaieyqvzjd";
    // generateKey(key);
    
    char msg[10240] = {0};
    FILE*f = fopen("./1.txt","r");
    int pos = 0;
    while(feof(f) == 0)
    {   
        int size = 0;
        fread(msg + pos,1024,1,f);
        pos += size;
    }
    fclose(f);
    // printf("%s\n",msg);
    
    
    Encrypt(msg,key);
    printf("加密后%s\n",msg);

    Decrypt(msg,key);
    printf("解密后:%s\n",msg);
    return 0;
}